<?php

class model_pk
{
	private $db = false;
	
	public function __construct() {
		require(BASEPATH.'config/database.php');
		$this->db = mysql_connect($db[ACTIVE_COMPETITION]['hostname'], $db[ACTIVE_COMPETITION]['username'], $db[ACTIVE_COMPETITION]['password']);
		if (! $this->db)
		{
			log_message('error', 'could not connect to database '.mysql_error());
		}
		mysql_select_db($db[ACTIVE_COMPETITION]['database'], $this->db);
		db_set_charset($db[ACTIVE_COMPETITION]['char_set'], $db[ACTIVE_COMPETITION]['dbcollat'], $this->db);

	}
	
	public function get_current_period() {
		$sql = "SELECT period, start_time, end_time, number FROM pk_competition WHERE state = 1 ORDER BY period DESC LIMIT 0, 1";
		
		$query = mysql_query ( $sql, $this->db );
		if (mysql_num_rows($query) > 0) {
			return mysql_fetch_object($query);
		}
		else {
			log_message('debug', "database operation get_period_by_time return null $sql");
			return null;
		}
	}
	
	public function add_new_pk_period($new_period) {
		$sql = "INSERT INTO pk_competition (period, state, start_time, end_time, number)
		VALUES ($new_period->period, 1, '$new_period->start_time', '$new_period->end_time', $new_period->number)";
		log_message('debug', "add_new_pk_period '$sql' ");
		$query = mysql_query ( $sql, $this->db );
		if (mysql_affected_rows($this->db) > 0) {
			log_message ( 'debug', "database operation add_new_pk_period success $sql" );
			return TRUE;
		} else {
			log_message ( 'error', "database operation add_new_pk_period $sql");
			return FALSE;
		}
	}
	public function pk_change_period($new_period) {
		$sql = "call proc_pk_change_period( $new_period ) ";
		
		log_message ( 'info', "database operation pk_change_period $sql" );
		$query = mysql_query ( $sql, $this->db );
		return $query;
	}
	public function pk_vote( $userid_, $time_ ) {
		$sql = "UPDATE pk SET votes = votes + 1, update_time = '$time_' WHERE userid = $userid_";
		mysql_query ( $sql, $this->db );
		if (mysql_affected_rows($this->db) > 0) {
			log_message ( 'debug', "database operation pk_vote  userid: $userid_ success" );
			return TRUE;
		} else {
			log_message ( 'error', "database operation is_pk_exist failed $sql");
			return FALSE;
		}
	}
	public function is_pk_exist( $userid_ ) {
		$sql = "SELECT userid FROM pk WHERE userid = $userid_ ";
		$query = mysql_query ( $sql, $this->db );
		if (mysql_num_rows($query) > 0) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	public function get_pk_by_userid( $userid_ ) {
		$sql = "SELECT userid, username, level, period, ranking, score, votes, pk_count, pked_count, plist, md5sum, create_time
				FROM pk
				WHERE userid = $userid_";
		
		log_message ( 'debug', "database operation get_pk_by_userid $sql " );
		$query = mysql_query ( $sql, $this->db );
		return $query;
	}
	public function get_pk_base_by_userid( $userid_ ) {
		$sql = "SELECT userid, ranking, score, votes, pk_count, pked_count
		FROM pk
		WHERE userid = $userid_";
	
		log_message ( 'debug', "database operation get_pk_base_by_userid $sql " );
		$query = mysql_query ( $sql, $this->db );
		return $query;
	}
	public function is_user_exist( $userid_ ) {
		$sql = "SELECT userid FROM user WHERE userid = $userid_ ";
		$query = mysql_query ( $sql, $this->db );
		if (mysql_num_rows($query) > 0) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	public function add_pk ( $userid_, $username_, $level_, $period_, $time_, $plist_, $md5sum_ ) {
	 	// get current ranking
	 	$sql = "SELECT count(userid) as count FROM pk WHERE score > 0";
	 	$query = mysql_query ( $sql, $this->db );
	 	$int_max = (1 << 31 - 1);
	 	if (mysql_num_rows($query) <= 0) {
	 		log_message ( 'debug', "database operation add_pk ranking 0" );
	 		$ranking = 1;
	 	}
	 	else {
	 		$row = mysql_fetch_object($query);
	 		$ranking = $row->count + 1;

	 		log_message ( 'debug', "database operation add_pk ranking query->count: $row->count" );
	 	}
	 	
 		$sql = "INSERT INTO pk( userid, username, period, level, ranking, score, votes, pk_count, pked_count, create_time, update_time, plist, md5sum ) VALUES ( $userid_, ".escape($this->db, $username_)." , $period_, $level_, $ranking, 0, 0, 0, 0, '$time_', '$time_', '$plist_', '$md5sum_' )";
 		$query = mysql_query ( $sql, $this->db );
 		if (mysql_affected_rows($this->db) > 0) {

			if ($this->is_user_exist($userid_)) {
 				$sql = "UPDATE user SET pk_count = pk_count + 1 WHERE userid = $userid_";
 				mysql_query ( $sql, $this->db );
 				if (mysql_affected_rows($this->db) <= 0) {
 					log_message('error', "database operation add_pk update user failed ".mysql_error().$sql);
 				}
 			}
 			else {
 				$sql = "INSERT INTO user(userid, username, cover_gril_best_ranking, pk_best_ranking) 
 						VALUES ($userid_, ".escape($this->db, $username_)." , $int_max, $int_max)";
 				mysql_query ( $sql, $this->db );
 				if (mysql_affected_rows($this->db) <= 0) {
 					log_message('error', "database operation add_pk insert user failed ".mysql_error().$sql);
 				}
 			}
 			$sql = "UPDATE pk_competition SET number = number + 1 WHERE period = $period_";
 			mysql_query ( $sql, $this->db );
 			if (mysql_affected_rows($this->db) <= 0) {
 				log_message('error', "database operation add_pk pk_competition update failed ".mysql_error().$sql);
 			}
 			return TRUE;
 		} else {
 			log_message ( 'error', "database operation add_pk failed ".mysql_error().$sql );
 			return FALSE;
 		}
	}
	public function get_pk_reward( $userid_ ) {
		$sql = "SELECT userid, period, ranking, score FROM pk_history WHERE userid = $userid_ AND has_get_reward = 0 ";
		$query = mysql_query ( $sql, $this->db );
		log_message ( 'debug', "database operation get_pk_reward  $sql" );
		return $query;
	}
	public function pk_has_get_reward ( $userid_, $period_ ) {
		$sql = "UPDATE pk_history SET has_get_reward = 1 WHERE userid = $userid_ AND period = $period_ ";
		mysql_query ( $sql, $this->db );
		if (mysql_affected_rows($this->db) > 0) {
			log_message ( 'debug', "database operation pk_has_get_reward  $sql" );
			return TRUE;
		} else {
			log_message ( 'error', "database operation pk_has_get_reward failed $sql");
			return FALSE;
		}
	}
	public function get_pk_reward_state($userid_, $period_) {
		$sql = "SELECT has_get_reward FROM pk_history WHERE userid = $userid_ AND period = $period_ ";
		$query = mysql_query ( $sql, $this->db );
		if (mysql_num_rows($query) > 0) {
			$row = mysql_fetch_object($query);
			return $row->has_get_reward;
		}
		else {
			return null;
		}
	}
	public function pk_change_score( $user1_, $score1_, $user2_, $score2_ ) {
		$sql = "call proc_pk_change_score( $user1_, $score1_, $user2_, $score2_ )";
		mysql_query ( $sql, $this->db );
		log_message ( 'debug', "database operation pk_change_score  $sql" );
		
		return TRUE;
	}
	public function get_pk_list_by_score ( $start_, $num_ ) {
		$sql = "SELECT userid, username, level, period, ranking, score, votes, pk_count, pked_count, plist, md5sum, create_time
				FROM pk
				ORDER BY score DESC
				LIMIT $start_, $num_ ";
	
		//		log_message ( 'debug', "database operation get_cover_gril_by_random $sql " );
		$query = mysql_query ( $sql, $this->db );
		return $query;
	}
	public function get_rank_list() {
		$sql = "SELECT userid, username, ranking, score FROM pk ORDER BY score desc LIMIT 0, 50";
		$query = mysql_query ( $sql, $this->db );
		return $query;
	}
	
	public function get_pk_global($userid_) {
		$sql = "SELECT userid, pk_count, pk_best_score, pk_best_ranking, pk_gold, pk_silver, pk_bronze FROM user WHERE userid = $userid";
		$query = mysql_query ( $sql, $this->db );
		if (mysql_num_rows($query) > 0) {
			return mysql_fetch_object($query);
		}
		else {
			return null;
		}
	}
}
